package org.interesting.joone;

import org.joone.engine.Monitor;
import org.joone.engine.NeuralNetEvent;
import org.joone.engine.NeuralNetListener;

/**
 * NN 监听器
 * 
 * @author wwxiang
 * @since 2018/6/25.
 */
public class DefaultNNListener implements NeuralNetListener {
	@Override
	public void netStarted(NeuralNetEvent e) {
		Monitor mon = (Monitor) e.getSource();
		StringBuilder sb = new StringBuilder("Network started for ");
		if (mon.isLearning()) {
			sb.append("training.");
		} else {
			sb.append("interrogation.");
		}
		System.out.println(sb);
	}

	@Override
	public void cicleTerminated(NeuralNetEvent e) {

	}

	@Override
	public void netStopped(NeuralNetEvent e) {
		Monitor mon = (Monitor) e.getSource();
		System.out.println("Network stopped. Last RMSE=" + mon.getGlobalError());
	}

	@Override
	public void errorChanged(NeuralNetEvent e) {
		Monitor mon = (Monitor) e.getSource();
		if (mon.getCurrentCicle() % 100 == 0) {
			System.out.println(
					"Epoch: " + (mon.getTotCicles() - mon.getCurrentCicle()) + " RMSE:" + mon.getGlobalError());
		}

	}

	@Override
	public void netStoppedError(NeuralNetEvent e, String error) {
		System.out.println("Network stopped due the following error: " + error);
	}
}
